/**   
* @Package com.wlyy.bcwlw.sys.area.service 
*/
package com.wlyy.bcwlw.sys.area.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wlyy.bcwlw.common.tree.BaseStaticTreeNode;
import com.wlyy.bcwlw.common.tree.StaticTree;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.SecurityFacade;
import com.wlyy.bcwlw.sys.area.dao.AreaCityDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaDistrictDAO;
import com.wlyy.bcwlw.sys.area.dao.AreaProvinceDAO;
import com.wlyy.bcwlw.sys.area.entity.AreaDTO;
import com.wlyy.bcwlw.sys.area.entity.AreaDistrictDTO;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;

/**
 * @ClassName. AreaService
 * @Description. TODO
 * @author zhangyang
 * @date 2016年1月18日 上午10:47:14
 * @version V1.0
 */
@Transactional
@Service
public class AreaService {
	@Autowired
	private AreaDAO areaDAO;
	@Autowired
	private AreaProvinceDAO areaProvinceDAO;
	@Autowired
	private AreaCityDAO areaCityDAO;
	@Autowired
	private AreaDistrictDAO areaDistrictDAO;
	@Autowired
	private UserDAO userDAO;
	
	private final String VirtualBaseTree ="VirtualBaseTree";
	
	public AreaDTO getAreaById(String areaId){
		return areaDAO.findAreaById(areaId);
		
		
	}
	
	
	public TreeNode getAreaTreeById(String id){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		
		nodeList.add(new BaseStaticTreeNode(VirtualBaseTree, "地区树", null, "base"));
		List<AreaDTO> areaList = areaDAO.queryListByCriteria(null, "scrq desc", null, null);
		
		if(areaList != null && areaList.size() !=0){
			for(AreaDTO areaDTO: areaList){
				BaseStaticTreeNode treeNode = null;
				treeNode =new BaseStaticTreeNode(areaDTO.getId(), areaDTO.getName(), VirtualBaseTree, "area");
				nodeList.add(treeNode);
			}
		}
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),id);
		tree.render();
		return tree.getRootNode();
	}
	
	public String removeAreaById(String areaId){
		 userDAO.setUserAreaIsNullByAreaId(areaId);
		 areaDAO.removeById(areaId);
		return "success";
	}
	
	 
	public AreaDTO saveArea(AreaDTO areaDTO){
		if(areaDTO.getDistrictId() !=null){
			AreaDistrictDTO areaDistrict = areaDistrictDAO.findById(areaDTO.getDistrictId());
			areaDTO.setName(areaDistrict.getDistrictName());
			//areaDTO.setBl(areaDistrict.getBl());
		}
		if(StringUtils.isEmpty(areaDTO.getId())){
			areaDAO.create(areaDTO);
		}else {
			AreaDTO oldAreaDTO = new AreaDTO();
			oldAreaDTO.setId(areaDTO.getId());
			int count = areaDAO.count(oldAreaDTO);
			if(count ==0){
				return null;
			}
			areaDAO.update(areaDTO);
		}
		return areaDTO;
	}
	
	public List<AreaDTO> getAreaList(){
		return areaDAO.queryListByCriteria(null, "scrq desc", null, null);
	}
	
	public AreaDTO getCurrentAreaDTO(){
		AreaDTO areaDTO = areaDAO.findAreaById(SecurityFacade.getCurrentAreaId());
		return areaDTO;
	}
}
